package com.jykj.modbus.manage;

import com.jykj.modbus.equipment.entity.CollectInformation;
import com.jykj.modbus.utils.MathUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/**
 * 鼎硕电气 DTS4921型三相电子式导轨智能电能表
 */
public class ElectricitySensor {

    /**
     * 解析返回的报文信息
     * @param hexStr 报文信息
     * @param type 返回结果类型(201:总电量;202:A相电压;203:B相电压;204:C相电压;
     *             205:A相电流;206:B相电流;207:C相电流;208:总功率;)
     * @return
     */
    public static Map<String , Object> electricityMessageConversion(String hexStr , String type){
        Map<String , Object> mapData = new HashMap<>();
        List<CollectInformation> collectData = new ArrayList<CollectInformation>();
        //解析地址码
        mapData.put("addressID" , hexStr.substring(0,2));
        //解析功能码
        mapData.put("functionID" , hexStr.substring(2,4));
        //有效字节数
        mapData.put("significantByte" , hexStr.substring(4,6));
        if (type.equals("201")){
            //总电量
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.01;
            Double valueData = value * 0.01;
            collect1.setTypeName("总电量");
            collect1.setValue(valueData.toString());
            collect1.setUnit("Kwh");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(14,16));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(16,18));
        }else if (type.equals("202")){
            //A相电压
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.1;
            mapData.put("value" , value.toString());
            collect1.setTypeName("A相电压");
            collect1.setValue(value.toString());
            collect1.setUnit("V");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("203")){
            //B相电压
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.1;
            mapData.put("value" , value.toString());
            collect1.setTypeName("B相电压");
            collect1.setValue(value.toString());
            collect1.setUnit("V");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("204")){
            mapData.put("typeName" , "C相电压");
            //C相电压
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.1;
            mapData.put("value" , value.toString());
            collect1.setTypeName("C相电压");
            collect1.setValue(value.toString());
            collect1.setUnit("V");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("205")){
            //A相电流
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.01;
            collect1.setTypeName("A相电流");
            collect1.setValue(value.toString());
            collect1.setUnit("A");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("206")){
            //B相电流
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.01;
            collect1.setTypeName("B相电流");
            collect1.setValue(value.toString());
            collect1.setUnit("A");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("207")){
            //C相电流
            CollectInformation collect1 = new CollectInformation();
            Double value = (MathUtils.hex16To10(hexStr.substring(6, 10)))*0.01;
            collect1.setTypeName("C相电流");
            collect1.setValue(value.toString());
            collect1.setUnit("A");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }else if (type.equals("208")){
            //总功率
            CollectInformation collect1 = new CollectInformation();
            Double value = MathUtils.hex16To10(hexStr.substring(6, 10));
            collect1.setTypeName("总功率");
            collect1.setValue(value.toString());
            collect1.setUnit("W");
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
            //解析校验码低位
            mapData.put("checkLow" , hexStr.substring(10,12));
            //解析校验码高位
            mapData.put("checkHigh" , hexStr.substring(12,14));
        }
        return mapData;
    }

    public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
        T t = null;
        try {
            t = entity.newInstance();
            for (Field field : entity.getDeclaredFields()) {
                if (map.containsKey(field.getName())) {
                    boolean flag = field.isAccessible();
                    field.setAccessible(true);
                    Object object = map.get(field.getName());
                    if (object != null && field.getType().isAssignableFrom(object.getClass())) {
                        field.set(t, object);
                    }
                    field.setAccessible(flag);
                }
            }
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

    public static void main(String[] args) {
        String str = "6821450020622168910833333333BC4B3633AE16";
        String str1 = "682145002062216891083333";
        String str2 = "FEFEFEFE6821450020622168910833333333C64B3633B816";
        Map<String, Object> map = electricityMessageConversion(str2, "201");
        System.out.println(map);
        CollectInformation collectInformation = mapToEntity(map, CollectInformation.class);
        System.out.println(collectInformation.getValue());
    }

}
